package com.controller;

import com.domain.Permission;
import com.domain.Role;
import com.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 项目名称：ssm21
 * 类 名 称：RoleController
 * 类 描 述：TODO
 * 创建时间：2020-01-19 20:27
 */
@RequestMapping("/role")
@Controller
public class RoleController {
    @Autowired
    private IRoleService iRoleService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Role> all = iRoleService.findAll();
        mv.addObject("roleList", all);
        mv.setViewName("role-list");
        return mv;
    }

    @RequestMapping("/save.do")
    public String save(Role role) throws Exception {
        iRoleService.save(role);
        return "redirect:findAll.do";
    }

    // 根据roleId查询role, 并查询出可以添加的权限
    @RequestMapping("/findUserByIdAndAllRole.do")
    public ModelAndView findRoleByIdAndAllPermission(@RequestParam(name = "id", required = true) String roleId) throws Exception {
        ModelAndView mv = new ModelAndView();
        // 根据roleId查询role
        Role role = iRoleService.findById(roleId);
        // 根据roleId查询可以添加的权限
        List<Permission> otherPermissions = iRoleService.findOtherPermissions(roleId);
        mv.addObject("role", role);
        mv.addObject("permissionList", otherPermissions);
        mv.setViewName("role-permission-add");
        return mv;
    }

    //给角色添加权限
    @RequestMapping("/addPermissionToRole.do")
    public String addPermissionToRole(@RequestParam(name = "ids", required = true) String[] permissionIds, @RequestParam(name = "roleId", required = true) String roleId) throws Exception {
        iRoleService.addPermissionToRole(permissionIds, roleId);
        return "redirect:findAll.do";
    }
}
